package base.loader
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.net.URLRequestMethod;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	public class LoaderBase extends BaseLoader
	{
		protected var _isSharedCache:Boolean;
		private var _loader:Loader;
		public static var loaderContext:LoaderContext;
		
		public function LoaderBase(loaderFlagType:String, id:int,url:String, isSharedCache:Boolean, _repeat:int=3, loaderVersion:int=0, level:int=0)
		{
			_loader=new Loader();
			_isSharedCache=isSharedCache;
			super(loaderFlagType, id, url, _repeat, null, URLRequestMethod.GET, loaderVersion, level);
		}
		
		override protected function __onCompleted(evt:Event):void
		{
			if(_urlLoader.data.length == 0)return;
			removeEvent();
//			_urlLoader.close();
			
			loadByBytes(_urlLoader.data);
			if(_isSharedCache) LoaderShared.Instance.cacheFile(_url, _urlLoader.data);
		}
		
		private function loadByBytes(bytes:ByteArray):void
		{
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, __onContentLoadComplete);
			_loader.loadBytes(bytes, loaderContext);
		}
		
		protected function __onContentLoadComplete(evt:Event):void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, __onContentLoadComplete);
			if(_loader.parent) _loader.parent.removeChild(_loader);
			_loader.unloadAndStop(true);
			_loader=null;
			
			_progress=1;
			_isComplete=true;
			_isLoading=false;
			_isSuccess=true;
			LoaderFactory.Instance.addLoad(_url);
			loaderComplete();
		}
		
		private var _completeTimer:Timer;
		/**
		 * 执行加载(如果直接执行加载,其加载将不会载入加载列表,也将会忽略自身在缓存中的已加载项)
		 */
		override public function executeGo():void
		{
			if(LoaderFactory.Instance.hasExistLoad(_url))
			{
				_isComplete=true;
			}
			
			if(_isComplete)
			{//如果已经加载成功过，发出完成事件(不能直接调用loaderComplete方法，如果相同路径加载超过一定数量时，执行过快会被播放器中止进程，所以使用timer进行缓冲)
				_completeTimer=new Timer(0);
				_completeTimer.addEventListener(TimerEvent.TIMER, __completeTimer);
				_completeTimer.start();
				return;
			}
			if(_isLoading)return;//如果在加载进行中，则返回
			
			if(_isSharedCache)
			{
				var ba:ByteArray=LoaderShared.Instance.getCachedFile(_url);
				if(ba && ba.length>0)
				{
					loadByBytes(ba);
				}
				else
				{
					super.executeGo();
				}
			}
			else
			{
				super.executeGo();
			}
		}
		
		private function __completeTimer(evt:TimerEvent):void
		{
			var t:Timer=evt.currentTarget as Timer;
			t.stop();
			t.removeEventListener(TimerEvent.TIMER, __completeTimer);
			t=null;
			if(_completeTimer)
			{
				_completeTimer.stop();
				_completeTimer.removeEventListener(TimerEvent.TIMER, __completeTimer);
				_completeTimer=null;
			}
			loaderComplete();
 		}
		
		override public function clone():BaseLoader
		{
			var loaderBase:LoaderBase=new LoaderBase(_loaderFlagType, _id, _url, _isSharedCache,_repeat,_loaderVersion);
			loaderBase.isComplete=_isComplete;
			loaderBase.isSuccess=_isSuccess;
//			loaderBase.isLoading=_isLoading;
			loaderBase.progress=_progress;
			return loaderBase;
		}
		
		override public function dispose():void
		{
			super.dispose();
			if(_completeTimer)
			{
				_completeTimer.stop();
				_completeTimer.removeEventListener(TimerEvent.TIMER, __completeTimer);
				_completeTimer=null;
			}
			if(_loader)
			{
				_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, __onContentLoadComplete);
				_loader.unloadAndStop(true);
//				if(_loader.loaderInfo && _loader.loaderInfo.bytes)_loader.loaderInfo.bytes.clear();
//				if(_loader.contentLoaderInfo.bytes)_loader.contentLoaderInfo.bytes.clear();
				if(_loader.parent) _loader.parent.removeChild(_loader);
			}
			_loader=null;
		}
	}
}